import javax.xml.bind.annotation.XmlType;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2023-10-09  15:34
 */
public class MyArrayList implements IList{
    public int[] elem;
    public int usedsize;
    public static final int DEFAULT_SIZE = 10;
    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }
    public MyArrayList(int capacity) {
        this.elem = new int[capacity];
    }

    @Override
    public void add(int data) {
        checkCapacity();
        this.elem[this.usedsize] = data;
        usedsize++;
    }

    @Override
    public void add(int pos, int data) {
        checkInsertPos(pos);
        for(int i = usedsize-1; i >= pos; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        usedsize++;
    }

    @Override
    public boolean contains(int toFind) {
        if(isEmpty()) {
            return false;
        }
        for(int i = 0; i < usedsize; i++) {
            if(this.elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        if(isEmpty()) {
            return -1;
        }
        for(int i = 0; i < usedsize; i++) {
            if(this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        //抛异常
        if(pos < 0 || pos >= pos) {
            System.out.println("不合法！");
        }
        //抛异常
        //if(isEmpty()) {
        //}
        return elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        //抛异常
        if(pos < 0 || pos >= usedsize) {
            System.out.println("错误！");
        }
        this.elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        int index = indexOf(toRemove);
        //抛异常
        if(index == -1) {
            System.out.println("错误！");
        }
        for(int i = index; i < usedsize-1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        usedsize--;
    }

    @Override
    public int size() {
        return this.usedsize;
    }

    @Override
    public void clear() {
        this.usedsize = 0;
        //引用类型
//        for(int i = 0; i < usedsize; i++) {
//            this.elem[i] = null;
//        }
//        usedsize = 0;
    }

    @Override
    public boolean isFull() {
        return this.elem.length == this.usedsize;
    }

    @Override
    public boolean isEmpty() {
        return usedsize == 0;
    }

    private void checkCapacity() {
        if(isFull()) {
            this.elem = Arrays.copyOf(elem,elem.length*2);
        }
    }
    //抛出错误
    private void checkInsertPos(int pos) {
        if(pos < 0 || pos > usedsize) {
            System.out.println("不合法");
        }
    }
    @Override
    public void display() {
        for(int i = 0; i < usedsize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }
}
